package com.neu.canteen.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
 
public class JdbcUtil {
	
	//硬编码
	   //配置信息写死在代码？
	   //方案:将配置信息放入到配置文件【xx.properties】,读取即可
	
	private static String driverClass;
	private static String url;
	private static String user;
	private static String password;
	static{
		
		Properties properties = new Properties();
		
		
		try {
			//可以加载资源文件夹下的文件
			properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
			
			driverClass = properties.getProperty("driverClass");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("password");
			
			Class.forName(driverClass);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConn() throws SQLException{
		return DriverManager.getConnection(url, user,password);
	}
	
	
	public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection conn) {
		try {
			if (resultSet != null) {
				resultSet.close();

			}
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
